package com.gec.service.Impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class BaseService {
    // {1}检测重复的方法。
    protected void checkUniqueException(Exception e, String[][] checks)
            throws Exception {
        String msg = e.getMessage();
        for (String[] info : checks) {
            boolean isHas = msg.contains(info[0]);
            if (isHas) {
                throw new SQLException(info[1]);
            }
        }
        //检测完没有发现唯一异常,把原有异常上抛
        throw e;
    }

    // {2}生成 uuid 字符串
    // uuid = 2d1f6b-63f01a-4403 [去除 "-"]
    protected String uuid() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "").substring(0, 8);
    }

    //{3}计数两个日期之间的天数
    protected int calDays(Date start, Date end) {
        long _end = end.getTime();
        long _start = start.getTime();
        long _len = _end - _start;
        int day = (int) (_len / 1000 / 3600 / 24);
        return day;
    }

    //{4}把日期转为字符串
    protected String dateToStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd");
        return sdf.format(date);
    }
}
